package com.lin.controller;

import com.lin.common.error.CustomRuntimeException;
import com.lin.common.rest.ResMsg;
import com.lin.controller.req.UpdateCustomerReqMsg;
import com.lin.service.customer.CustomerGroupServiceImpl;
import com.lin.service.customer.CustomerService;
import com.lin.service.login.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

@Api(tags = "用户入口")
@RestController
@RequestMapping("/msvc")
public class CustomerController {
    Logger log = LoggerFactory.getLogger(CustomerController.class);

    @Autowired
    private LoginService loginService;

    @Autowired
    private CustomerService customerService;

    /**
     * 该接口不涉及头像更新
     * @param reqMsg
     * @return
     * @throws CustomRuntimeException
     */
    @ApiOperation("更新用户信息接口")
    @PostMapping("/updatecustomer")
    public ResMsg<Integer> updateCustomer(@Valid @RequestBody UpdateCustomerReqMsg reqMsg) throws CustomRuntimeException {
        ResMsg<Integer> urs = new ResMsg<>();
        Integer i = customerService.updateCustomer(reqMsg);
        return urs.withData(i);
    }

    @ApiOperation("更新头像接口")
    @PostMapping("/updateportrait/{id}")
    public ResMsg<Integer> updatePortrait(@PathVariable Long id, @RequestBody byte[] portrait) throws CustomRuntimeException, IOException {
        ResMsg<Integer> urs = new ResMsg<>();
        Integer i = customerService.updatePortrait(id,portrait);
        return urs.withData(i);
    }

    @ApiOperation("登出接口")
    @GetMapping("/logout")
    public ResMsg<Integer> logout() throws CustomRuntimeException {
        return ResMsg.ok(loginService.logout());
    }
}
